import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javaframework.JavaGameContext;
import javaframework.JavaGameContext.JavaScreen;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;

import framework.Color;
import framework.GraphicsWrapper;
import framework.ResourcesWrapper;
import gamebackend.CloneShip;
import gamebackend.LaserShip;
import gamebackend.Ship;


public class JavaStartScreen extends JavaScreen {

	
	private final JTextArea my_text;
	private final JRadioButton[] my_ships;
	private final String[] my_descriptions;
	private final JavaGameContext my_context;
	public JavaStartScreen(JavaGameContext javaGameContext) {
		javaGameContext.super(javaGameContext);
		final ResourcesWrapper res = my_game.getResourcesWrapper();
		my_context = javaGameContext;
		
		JPanel p = new JPanel();
		final JRadioButton laserButton = new JRadioButton("Laser", true);
		final JRadioButton cloneButton = new JRadioButton("Clone");
		my_ships = new JRadioButton[]{laserButton, cloneButton};
		my_descriptions = new String[]{
				// laser ship
				"Blasts away obstacles with a powerful laser.",
				// clone ship
				"Teleports to holographic projections."
				};
		my_text = new JTextArea(3, 0);
		my_text.setEditable(false);
		my_text.setLineWrap(true);
		my_text.setWrapStyleWord(true);
		my_text.setText(my_descriptions[0]);
		ButtonGroup group = new ButtonGroup();
		ActionListener shipListener = new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				int i = 0;
				for (; my_ships[i] != e.getSource(); i++);
				my_text.setText(my_descriptions[i]);
				
			}};
		for (JRadioButton b : my_ships) {
			group.add(b);
			p.add(b);
			b.addActionListener(shipListener);
		}
		JPanel root = new JPanel();
		root.add(new JLabel("Select Ship"));
		root.setLayout(new BoxLayout(root, BoxLayout.PAGE_AXIS));
		root.add(p);
		JButton startButton = new JButton("Start");
		startButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				clearSwing();
				final Class<? extends Ship> c;
				if (laserButton.isSelected()) {
					c = LaserShip.class;
				} else {
					c = CloneShip.class;
				}
				JavaStartScreen.this.my_game.setScreen(new JavaPlayScreen(my_context, c));
			}
		});
		root.add(my_text);
		root.add(startButton);
		clearSwing();
		addSwing(root);
		
	}

	@Override
	public void step(int the_deltaTime) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void render() {
		final GraphicsWrapper g = my_game.getGraphicsWrapper();
		g.setColor(Color.BLACK);
		g.fillRect(0, 0, g.getWidth(), g.getHeight());
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

	

}
